﻿using System;
using iRealtor.Maps.DataAccess.Dao;
using iRealtor.Maps.DataAccess.Models;
using log4net;

namespace iRealtor.Maps.DataAccess.Services
{
	public class MySQLBaseService
	{
		protected IDao dao = null;
		protected readonly ILog logger = LogManager.GetLogger(typeof(MySQLBaseService));

		public MySQLBaseService()
		{
			try
			{
				dao = new MySqlDaoFactory().CreateDao();
			}
			catch (Exception ex)
			{
				logger.Error(GetType(), ex);
				throw new Exception("Can't create DAO", ex);
			}
		}

		protected const string ENSURE_LOAD_EXCEPTION = "{0}({1}) was not found in database.";

		protected TEntity EnsureLoad<TEntity>(object key) where TEntity : Entity
		{
			TEntity res = dao.Load<TEntity>(key);

			if (res == null)
				throw new Exception(string.Format(ENSURE_LOAD_EXCEPTION, typeof(TEntity).Name, key));

			return res;
		}

		public TEntity Load<TEntity>(object key) where TEntity : Entity
		{
			TEntity res = null;
			try
			{
				res = dao.Load<TEntity>(key);
			}
			catch (Exception ex)
			{
				logger.Error(GetType(), ex);
				res = null;
			}
			return res;
		}

		public bool Save<TEntity>(TEntity obj) where TEntity : Entity
		{
			bool res = true;
			try
			{
				dao.BeginTransaction();

				dao.Save<TEntity>(obj);

				dao.CommitTransaction();
			}
			catch (Exception ex)
			{
				res = false;
				dao.RollbackTransaction();
				logger.Error(GetType(), ex);
			}
			return res;
		}
	}
}
